*********************************************************************************
* Table 8. Changes in interest about wife's LFP – IPWRA (analytic SEs, main text) + OLS (appendix)
*********************************************************************************


**# Definitions

* Main sample indicator::
local samples sample1 /*sample1_any sample1_alone sample1_other sample1_both*/

* Baseline belief block (used in diagnostics)
local bel2  fob2 sob2 sob2m sob2p

* "Strata" covariates used in absorb() and IPW models
local strat inactiveF employedF fob2M

* Outcomes: comment/uncomment to include/exclude
local yvars y1 y2 y3 y4 y5


* Initial control variables (diagnostics)
local xvars female c1 c2_2_rp c3 c5 f6 age_dif  level_1 level_2 edu_WmoreH  inactiveM unemployedM estrato_12 estrato_3 tec_2 

		foreach sample in `samples' {

********************************************************
* Diagnostics for attrition (who is in sample3_ext)
********************************************************

di "***************************************"
di "`sample'"
ta D female if `sample' ==1,m

* Diagnostics for attrition (who is in sample1_ext): only significant c5 level_1 tec_2 (barely sob2p)
reghdfe `sample' D `xvars' `bel2', absorb(`strat') vce(cluster id)
	* sample1_ext : female, c5, age, agedif, (level_1), inactiveM, tec_2, 
	* sample1_any: female, c1, c5, f6, age_dif, level_2, inactiveM, tec_2, 
	* sample1_alone: female, c1, tec_2,sob2m (-)
	* sample1_other: female, c1, level_1, tec_2 
	* sample1_both: c3, c5, f6, level_1, edu_WmoreH, tec_2,
	
* Diagnostics for selective exposure (who takes D): only significant tec_2
reghdfe D `xvars' `bel2' if `sample' == 1, absorb(`strat') vce(cluster id)
	*sample1 : nothing
	*sample1_any: estrato12 (-), tec_2 (-), sob2p (-)
	*sample1_alone: estrato12 (-), estrato_3 (-), tec_2 (-), sob2m (+)
	*sample1_other: estrato12 (-), estrato_3 (-), tech_2 (-),
	*sample1_both: level_1, unemployedM, 
	
* logit : P(second répond) = f(sexe du premier) 
logit second_responds i.first_finisher##D edu_WmoreH  inactiveM unemployedM estrato_12 estrato_3 tec_2  c1 c2_2_rp c3 c5 f6 age_dif if tag_couple & n_spouses_chat >= 1
	

di "***************************************"
		
		

********************************************************************************
* IPW weights for selection (attrition weights for being in `sample')
********************************************************************************
cap drop ps_attrition 
cap drop w_*
cap drop wa_*
cap drop ws_*

	probit `sample' D `xvars' `bels' `strat', vce(cluster id)
	*probit `sample' D `xvars' `bels' `strat', vce(cluster id)

predict ps_attrition, pr 

gen w_`sample' = 1 / ps_attrition
quietly summ w_`sample'
local p_uncond = r(mean)
gen ws_`sample' = `p_uncond' / ps_attrition   // normalized (if needed)
gen wa_`sample' = 1                           // unweighted

* Default selection weight (you can switch to ws_ if you prefer normalized)
local weight w_`sample'
summ `weight' 
bys D : su `weight' if sample1 ==1
bys D : su `weight' ws_`sample' ps_attrition if sample1 ==1
local reps $reps

	
		
local vars_sample3 `xvars' `bel2'
local reps $reps

********************************************************************************
* STORAGE MACROS FOR THE THREE TABLES
********************************************************************************
eststo clear
local j = 1

local estlist_ols_base ""   // 7A: Baseline OLS (no weights) – one model per outcome
local mtitles_ols_base ""

local estlist_ols_ipw ""    // 7B: OLS + selection IPW – one model per outcome
local mtitles_ols_ipw ""

local estlist_ipwra ""      // 7C: IPWRA ATT/PO + OLS-IPW D – one model per outcome
local mtitles_ipwra ""


********************************************************************************
* LOOP OVER OUTCOMES
********************************************************************************



foreach y of local yvars {

   /********************************************************************
     * 1. BASELINE OLS (no selection weights): pooled model only
     *    Male/Female enter as stats rows
     ********************************************************************/

    * (1) Pooled OLS, no interaction, control for female
    reghdfe `y' D `vars_sample3' female ///
        if `sample' == 1, absorb(`strat') vce(cluster id)
   scalar N_all = e(N)
    eststo OLS_base_`j'

    *** Means of Y by subgroup (unweighted)
    quietly summarize `y' if `sample' == 1
    scalar ymean_all  = r(mean)

    quietly summarize `y' if `sample' == 1 & female == 0
    scalar ymean_male = r(mean)

    quietly summarize `y' if `sample' == 1 & female == 1
    scalar ymean_fem  = r(mean)

    * (2) OLS – males only
    reghdfe `y' D `vars_sample3' ///
        if `sample' == 1 & female == 0, ///
        absorb(`strat') vce(cluster id)
    scalar N_male     = e(N)
    scalar b_D_male   = _b[D]
    scalar se_D_male  = _se[D]
    scalar t_D_male   = b_D_male / se_D_male
    scalar p_D_male   = 2*(1 - normal(abs(t_D_male)))
	
    * (3) OLS – females only
    reghdfe `y' D `vars_sample3' ///
        if `sample' == 1 & female == 1, ///
        absorb(`strat') vce(cluster id)
    scalar N_fem      = e(N)
    scalar b_D_fem    = _b[D]
    scalar se_D_fem   = _se[D]
    scalar t_D_fem    = b_D_fem / se_D_fem
    scalar p_D_fem    = 2*(1 - normal(abs(t_D_fem)))

    ********************************************************************
    * RITEST p-values for OLS coefficients on D
    ********************************************************************
    * (4) RI – pooled, coefficient on D
    capture noisily ritest D _b[D], ///
        cluster(id) strata(`strat') reps(`reps') : ///
        reghdfe `y' D `vars_sample3' female ///
        if `sample' == 1, ///
        absorb(`strat') vce(cluster id)

  scalar p_ri_all = .
    if (_rc == 0) {
        matrix P_all = r(p)
        scalar p_ri_all = round(P_all[1,1], .001)
    }

    * (5) RI – males only
    capture noisily  ritest D _b[D], ///
        cluster(id) strata(`strat') reps(`reps') : ///
        reghdfe `y' D `vars_sample3' ///
        if `sample' == 1 & female == 0, ///
        absorb(`strat') vce(cluster id)

    scalar p_ri_male = .
    if (_rc == 0) {
        matrix P_m = r(p)
        scalar p_ri_male = round(P_m[1,1], .001)
    }

    * (6) RI – females only
    capture noisily     ritest D _b[D], ///
        cluster(id) strata(`strat') reps(`reps') : ///
        reghdfe `y' D `vars_sample3' ///
        if `sample' == 1 & female == 1, ///
        absorb(`strat') vce(cluster id)

    scalar p_ri_fem = .
    if (_rc == 0) {
        matrix P_f = r(p)
        scalar p_ri_fem = round(P_f[1,1], .001)
    }
    ********************************************************************
    * Attach OLS, N, and RI scalars to the pooled OLS estimate
    ********************************************************************
   estimates restore OLS_base_`j'
    estadd scalar N_all      = N_all,      replace
    estadd scalar N_male     = N_male,     replace
    estadd scalar N_fem      = N_fem,      replace

    estadd scalar ymean_all  = ymean_all,  replace
    estadd scalar ymean_male = ymean_male, replace
    estadd scalar ymean_fem  = ymean_fem,  replace

    estadd scalar b_D_male   = b_D_male,   replace
    estadd scalar se_D_male  = se_D_male,  replace
    estadd scalar p_D_male   = p_D_male,   replace

    estadd scalar b_D_fem    = b_D_fem,    replace
    estadd scalar se_D_fem   = se_D_fem,   replace
    estadd scalar p_D_fem    = p_D_fem,    replace

    estadd scalar p_ri_all   = p_ri_all,   replace
    estadd scalar p_ri_male  = p_ri_male,  replace
    estadd scalar p_ri_fem   = p_ri_fem,   replace

    eststo OLS_base_`j'

    /********************************************************************
     * 2. OLS WITH SELECTION IPW WEIGHTS: pooled model only
     ********************************************************************/


    *** (2.1) Pooled weighted OLS – All
    reghdfe `y' D `vars_sample3' `y' ///
        if `sample' == 1 [pweight = `weight'], ///
        absorb(`strat') vce(cluster id)
    scalar N_all_w = e(N)
    eststo OLS_ipw_`j'

    *** Weighted means of Y by subgroup
    quietly summarize `y' [aweight = `weight'] if `sample' == 1
    scalar ymean_all_w  = r(mean)

    quietly summarize `y' [aweight = `weight'] if `sample' == 1 & female == 0
    scalar ymean_male_w = r(mean)

    quietly summarize `y' [aweight = `weight'] if `sample' == 1 & female == 1
    scalar ymean_fem_w  = r(mean)

    *** (2.2) Weighted OLS – Male
    reghdfe `y' D `vars_sample3' `y' ///
        if `sample' == 1 & female == 0 [pweight = `weight'], ///
        absorb(`strat') vce(cluster id)
    scalar N_male_w     = e(N)
    scalar b_D_male_w   = _b[D]
    scalar se_D_male_w  = _se[D]
    scalar t_D_male_w   = b_D_male_w / se_D_male_w
    scalar p_D_male_w   = 2*(1 - normal(abs(t_D_male_w)))

    *** (2.3) Weighted OLS – Female
    reghdfe `y' D `vars_sample3' `y' ///
        if `sample' == 1 & female == 1 [pweight = `weight'], ///
        absorb(`strat') vce(cluster id)
    scalar N_fem_w      = e(N)
    scalar b_D_fem_w    = _b[D]
    scalar se_D_fem_w   = _se[D]
    scalar t_D_fem_w    = b_D_fem_w / se_D_fem_w
    scalar p_D_fem_w    = 2*(1 - normal(abs(t_D_fem_w)))

    *** Attach scalars to pooled weighted OLS model for 6B
    estimates restore OLS_ipw_`j'
    estadd scalar N_all      = N_all_w,      replace
    estadd scalar N_male     = N_male_w,     replace
    estadd scalar N_fem      = N_fem_w,      replace

    estadd scalar ymean_all  = ymean_all_w,  replace
    estadd scalar ymean_male = ymean_male_w, replace
    estadd scalar ymean_fem  = ymean_fem_w,  replace

    estadd scalar b_D_male   = b_D_male_w,   replace
    estadd scalar se_D_male  = se_D_male_w,  replace
    estadd scalar p_D_male   = p_D_male_w,   replace

    estadd scalar b_D_fem    = b_D_fem_w,    replace
    estadd scalar se_D_fem   = se_D_fem_w,   replace
    estadd scalar p_D_fem    = p_D_fem_w,    replace

    eststo OLS_ipw_`j'
	
	
	 /********************************************************************
     * 3. IPWRA – ATT/PO for All, Male, Female (analytic SEs)
     *    Use selection weights as pweights; attach to pooled weighted model
     ********************************************************************/
   *** (3.1) IPWRA – All
    teffects ipwra (`y' `strat') ///
                   (D `vars_sample3' `strat') ///
                   if `sample' == 1 [pweight = `weight'], ///
                   atet vce(cluster id)

    matrix bA = e(b)
    matrix VA = e(V)

    scalar ATET_total    = bA[1,1]
    scalar ATET_se_total = sqrt(VA[1,1])
    scalar ATET_pv_total = 2*(1 - normal(abs(ATET_total/ATET_se_total)))

    scalar PO_total      = bA[1,2]
    scalar PO_se_total   = sqrt(VA[2,2])
    scalar PO_pv_total   = 2*(1 - normal(abs(PO_total/PO_se_total)))

      *** (3.2) IPWRA – Male
    teffects ipwra (`y' `strat') ///
                   (D `vars_sample3' `strat') ///
                   if `sample' == 1 & female == 0 [pweight = `weight'], ///
                   atet vce(cluster id)

    matrix bA = e(b)
    matrix VA = e(V)

    scalar ATET_male    = bA[1,1]
    scalar ATET_se_male = sqrt(VA[1,1])
    scalar ATET_pv_m    = 2*(1 - normal(abs(ATET_male/ATET_se_male)))

    scalar PO_male      = bA[1,2]
    scalar PO_se_male   = sqrt(VA[2,2])
    scalar PO_pv_male   = 2*(1 - normal(abs(PO_male/PO_se_male)))

    *** (3.3) IPWRA – Female
    teffects ipwra (`y' `strat') ///
                   (D `vars_sample3' `strat') ///
                   if `sample' == 1 & female == 1 [pweight = `weight'], ///
                   atet vce(cluster id)

    matrix bA = e(b)
    matrix VA = e(V)

    scalar ATET_female     = bA[1,1]
    scalar ATET_se_female  = sqrt(VA[1,1])
    scalar ATET_pv_female  = 2*(1 - normal(abs(ATET_female/ATET_se_female)))

    scalar PO_female       = bA[1,2]
    scalar PO_se_female    = sqrt(VA[2,2])
    scalar PO_pv_female    = 2*(1 - normal(abs(PO_female/PO_se_female)))

 *** Create IPWRA container: copy pooled weighted OLS and attach ATT/PO scalars
    estimates restore OLS_ipw_`j'
    eststo IPWRA_`j'

    estimates restore IPWRA_`j'
    estadd scalar ATET_all    = ATET_all,    replace
    estadd scalar ATET_se_all = ATET_se_all, replace
    estadd scalar ATET_pv_all = ATET_pv_all, replace

    estadd scalar PO_all      = PO_all,      replace
    estadd scalar PO_se_all   = PO_se_all,   replace
    estadd scalar PO_pv_all   = PO_pv_all,   replace

    estadd scalar ATET_male     = ATET_male,     replace
    estadd scalar ATET_se_male  = ATET_se_male,  replace
    estadd scalar ATET_pv_male  = ATET_pv_male,  replace

    estadd scalar PO_male       = PO_male,       replace
    estadd scalar PO_se_male    = PO_se_male,    replace
    estadd scalar PO_pv_male    = PO_pv_male,    replace

    estadd scalar ATET_fem      = ATET_fem,      replace
    estadd scalar ATET_se_fem   = ATET_se_fem,   replace
    estadd scalar ATET_pv_fem   = ATET_pv_fem,   replace

    estadd scalar PO_fem        = PO_fem,        replace
    estadd scalar PO_se_fem     = PO_se_fem,     replace
    estadd scalar PO_pv_fem     = PO_pv_fem,     replace

    eststo IPWRA_`j'

    ********************************************************************
    * Append models and column titles
    ********************************************************************
    local ylab : variable label `y'
    if "`ylab'" == "" local ylab "`y'"

    local estlist_ols_base "`estlist_ols_base' OLS_base_`j'"
    local mtitles_ols_base `"`mtitles_ols_base' "`ylab'""'

    local estlist_ols_ipw  "`estlist_ols_ipw'  OLS_ipw_`j'"
    local mtitles_ols_ipw  `"`mtitles_ols_ipw'  "`ylab'""'

    local estlist_ipwra    "`estlist_ipwra'    IPWRA_`j'"
    local mtitles_ipwra    `"`mtitles_ipwra'    "`ylab'""'

    local j = `j' + 1

  
}

*=====================================================
* Export MAIN table – IPWRA (analytic SEs)
*=====================================================

*=====================================================
* Table 8A – BASELINE OLS (no selection weights)
*=====================================================
#delimit ;
local opts_ols_base style(tex)  
  varlabels(_cons Constant D "Treatment" female "Female (=1)") 
  cells(b(star fmt(%9.3f)) se(par)) 
  stats(N_all N_male N_fem 
        ymean_all ymean_male ymean_fem
        b_D_male se_D_male p_D_male
        b_D_fem  se_D_fem  p_D_fem
        p_ri_all p_ri_male p_ri_fem,
        fmt(%9.3f)
        label("N (All)" 
              "N (Male)"
              "N (Female)"
              "Mean Dep. Var (All)"
              "Mean Dep. Var (Male)"
              "Mean Dep. Var (Female)"
              "OLS β(D) Male"    "OLS SE Male"    "OLS p-val Male"
              "OLS β(D) Female"  "OLS SE Female"  "OLS p-val Female"
              "RI p-val D (All)" "RI p-val D (Male)" "RI p-val D (Female)")) 
  label collabels(none) msign(--) nolz varwidth(1) modelwidth(14) 
  starlevels(* 0.10 ** 0.05 *** 0.01)
;
#delimit cr

estout `estlist_ols_base' using "$outdir/Table8_InterestWifeLFP_OLS_baseline_`sample'.tex", ///
    replace `opts_ols_base' mlabels(`mtitles_ols_base')

*=====================================================
* Table 8B – OLS WITH SELECTION IPW WEIGHTS
*=====================================================
#delimit ;
local opts_ols_ipw style(tex)  
  varlabels(_cons Constant D "Treatment" female "Female (=1)") 
  cells(b(star fmt(%9.3f)) se(par)) 
  stats(N_all N_male N_fem 
        ymean_all ymean_male ymean_fem
        b_D_male se_D_male p_D_male
        b_D_fem  se_D_fem  p_D_fem,
        fmt(%9.3f)
        label("N (All)" 
              "N (Male)"
              "N (Female)"
              "Mean Dep. Var (All, weighted)"
              "Mean Dep. Var (Male, weighted)"
              "Mean Dep. Var (Female, weighted)"
              "OLS-IPW β(D) Male"    "OLS-IPW SE Male"    "OLS-IPW p-val Male"
              "OLS-IPW β(D) Female"  "OLS-IPW SE Female"  "OLS-IPW p-val Female")) 
  label collabels(none) msign(--) nolz varwidth(1) modelwidth(14) 
  starlevels(* 0.10 ** 0.05 *** 0.01)
;
#delimit cr

estout `estlist_ols_ipw' using "$outdir/Table8_InterestWifeLFP_OLS_IPW_`sample'.tex", ///
    replace `opts_ols_ipw' mlabels(`mtitles_ols_ipw')

*=====================================================
* Table 8C – IPWRA (analytic SEs), only D in coefficient panel
*=====================================================
#delimit ;
local opts_ipwra style(tex)  
  cells(b(star fmt(%9.3f)) se(par)) 
  stats(ATET_all ATET_se_all ATET_pv_all 
        PO_all   PO_se_all   PO_pv_all
        ATET_male ATET_se_male ATET_pv_male
        PO_male   PO_se_male   PO_pv_male
        ATET_fem  ATET_se_fem  ATET_pv_fem
        PO_fem    PO_se_fem    PO_pv_fem,
        fmt(%9.3f)
        label("ATT All"      "Std. Err. ATT All"   "P-value ATT All"
              "PO All"       "Std. Err. PO All"    "P-value PO All"
              "ATT Male"     "Std. Err. ATT Male"  "P-value ATT Male"
              "PO Male"      "Std. Err. PO Male"   "P-value PO Male"
              "ATT Female"   "Std. Err. ATT Female""P-value ATT Female"
              "PO Female"    "Std. Err. PO Female" "P-value PO Female")) 
  label collabels(none) msign(--) nolz varwidth(1) modelwidth(14) 
  starlevels(* 0.10 ** 0.05 *** 0.01)
;
#delimit cr

estout `estlist_ipwra' using "$outdir/Table8_InterestWifeLFP_IPWRA_main_`sample'.tex", ///
    replace `opts_ipwra' mlabels(`mtitles_ipwra') ///
    keep(D) varlabels(D "Treatment") 

}
